package components.contorls.superClass
{
    import components.contorls.RadioButton;
    import components.core.UIComponent;
    import components.core.zn_internal;
    
    import flash.events.Event;
    
    import mx.events.FlexEvent;

    [Event(name="selectionChange", type="mx.events.FlexEvent")]
    public class BaseRadioGroup extends UIComponent
    {
        private var _selectedRaidoButton:BaseRadioButton;

        private var _raidoButtonList:Array = [];

        private var _radioButtonNameList:Array = [];

        private var _selectedRadioButtonName:String;

        public function BaseRadioGroup()
        {
            super();
        }

        [Inspectable(defaultValue="", type="Array")]
        public function get radioButtonNameList():Array
        {
            return _radioButtonNameList;
        }

        public function set radioButtonNameList(value:Array):void
        {
            var radioButton:RadioButton;
            if (_radioButtonNameList)
			{
				_radioButtonNameList=null;
				selectedRaidoButton=null;
            }

            _raidoButtonList = [];
            if (parent)
            {
                _radioButtonNameList = value;

                for (var i:int = 0; i < _radioButtonNameList.length; i++)
                {
                    var elementName:String = _radioButtonNameList[i] as String;

                    radioButton = parent.getChildByName(elementName) as RadioButton;
                    addRadioButton(radioButton);
                }
            }
        }

        [Inspectable(defaultValue="")]
        public function get selectedRadioButtonName():String
        {
            return _selectedRadioButtonName;
        }

        public function set selectedRadioButtonName(value:String):void
        {
            if (parent)
            {
                _selectedRadioButtonName = value;
                selectedRaidoButton = parent.getChildByName(_selectedRadioButtonName) as RadioButton;
            }
        }

        public function get selectedRaidoButton():BaseRadioButton
        {
            return _selectedRaidoButton;
        }

        public function set selectedRaidoButton(value:BaseRadioButton):void
        {
            if (value == _selectedRaidoButton)
                return;

            if (_selectedRaidoButton)
            {
                _selectedRaidoButton.selected = false;
                _selectedRaidoButton = null;
            }

            if (value)
            {
                _selectedRaidoButton = value;
                _selectedRaidoButton.selected = true;
            }

            dispatchEvent(new FlexEvent(FlexEvent.SELECTION_CHANGE));
        }

        public function addRadioButton(radioButton:BaseRadioButton):void
        {
            if (radioButton.radioGroup)
            {
                radioButton.radioGroup.removeRadioButton(radioButton);
                radioButton.zn_internal::radioGroup = null;
            }

            _raidoButtonList.push(radioButton);
            radioButton.zn_internal::radioGroup = this;

            if (radioButton.selected)
                selectedRaidoButton = radioButton;
        }

        public function removeRadioButton(radioButton:BaseRadioButton):void
        {
            for (var i:int = 0; i < _raidoButtonList.length; i++)
            {
                if (_raidoButtonList[i] == radioButton)
                {
                    _raidoButtonList.splice(i, 1);
                    break;
                }
            }

            if (_raidoButtonList.length > 0 && _selectedRaidoButton == radioButton)
            {
                selectedRaidoButton = _raidoButtonList[0] as BaseRadioButton;
            }
            else
            {
                selectedRaidoButton = null;
            }
        } 
		
		protected override function frameConstructedHandler(event:Event):void
		{
			super.frameConstructedHandler(event);
			
			visible=false;
		}

    }
}